/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web.game;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.game.NumberGame;
import com.boxin.ims.modules.wechat.entity.game.NumberGameConfig;
import com.boxin.ims.modules.wechat.service.game.NumberGameConfigService;

/**
 * 数字抽奖游戏配置Controller
 * @author Jakemanse
 * @version 2014-08-22
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/game/numberGameConfig")
public class NumberGameConfigController extends BaseController {

	@Autowired
	private NumberGameConfigService numberGameConfigService;
	
	@ModelAttribute
	public NumberGameConfig get(@RequestParam(required=false) Long id) {
		if (id != null){
			return numberGameConfigService.get(id);
		}else{
			return new NumberGameConfig();
		}
	}
	
	@RequiresPermissions("wechat:game:numberGameConfig:view")
	@RequestMapping(value = {"list", ""})
	public String list(NumberGameConfig numberGameConfig, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			numberGameConfig.setUser(user);
		}
        Page<NumberGameConfig> page = numberGameConfigService.find(new Page<NumberGameConfig>(request, response), numberGameConfig); 
        model.addAttribute("page", page);
		return "modules/wechat/game/numberGameConfigList";
	}

	@RequiresPermissions("wechat:game:numberGameConfig:view")
	@RequestMapping(value = "form")
	public String form(NumberGameConfig numberGameConfig, Model model) {
	
		User user = UserUtils.getUser();
		NumberGameConfig temp = numberGameConfigService.getByUserId(user.getId());
		if(temp!=null){
			numberGameConfig = temp;
		}else{
			numberGameConfig.setUser(user);
		}
		
		
		model.addAttribute("numberGameConfig", numberGameConfig);
		return "modules/wechat/game/numberGameConfigForm";
	}

	@RequiresPermissions("wechat:game:numberGameConfig:edit")
	@RequestMapping(value = "save")
	public String save(NumberGameConfig numberGameConfig, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, numberGameConfig)){
			return form(numberGameConfig, model);
		}
		numberGameConfigService.save(numberGameConfig);
		addMessage(redirectAttributes, "保存数字抽奖游戏配置'" + numberGameConfig.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/game/numberGameConfig/form?repage";
	}
	
	@RequiresPermissions("wechat:game:numberGameConfig:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		numberGameConfigService.delete(id);
		addMessage(redirectAttributes, "删除数字抽奖游戏配置成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/game/numberGameConfig/?repage";
	}

}
